//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tencent.bugly.crashreport.crash;

import android.content.Context;
import android.os.Process;
import com.tencent.bugly.crashreport.common.strategy.StrategyBean;
import com.tencent.bugly.crashreport.common.strategy.a;
import com.tencent.bugly.proguard.x;
import com.tencent.bugly.proguard.y;
import com.tencent.bugly.proguard.z;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.HashMap;

public final class e implements UncaughtExceptionHandler {
    private Context a;
    private b b;
    private a c;
    private com.tencent.bugly.crashreport.common.info.a d;
    private UncaughtExceptionHandler e;
    private UncaughtExceptionHandler f;
    private boolean g = false;
    private static String h = null;
    private static final Object i = new Object();
    private int j;

    public e(Context var1, b var2, a var3, com.tencent.bugly.crashreport.common.info.a var4) {
        this.a = var1;
        this.b = var2;
        this.c = var3;
        this.d = var4;
    }

    public final synchronized void a() {
        if (this.j >= 10) {
            x.a("java crash handler over %d, no need set.", new Object[]{10});
        } else {
            this.g = true;
            UncaughtExceptionHandler var1;
            if ((var1 = Thread.getDefaultUncaughtExceptionHandler()) != null) {
                String var2 = this.getClass().getName();
                String var3 = var1.getClass().getName();
                if (var2.equals(var3)) {
                    return;
                }

                if ("com.android.internal.os.RuntimeInit$UncaughtHandler".equals(var1.getClass().getName())) {
                    x.a("backup system java handler: %s", new Object[]{var1.toString()});
                    this.f = var1;
                    this.e = var1;
                } else {
                    x.a("backup java handler: %s", new Object[]{var1.toString()});
                    this.e = var1;
                }
            }

            Thread.setDefaultUncaughtExceptionHandler(this);
            ++this.j;
            x.a("registered java monitor: %s", new Object[]{this.toString()});
        }
    }

    public final synchronized void b() {
        this.g = false;
        x.a("close java monitor!", new Object[0]);
        if (Thread.getDefaultUncaughtExceptionHandler().getClass().getName().contains("bugly")) {
            x.a("Java monitor to unregister: %s", new Object[]{this.toString()});
            Thread.setDefaultUncaughtExceptionHandler(this.e);
            --this.j;
        }

    }

    private CrashDetailBean b(Thread var1, Throwable var2, boolean var3, String var4, byte[] var5) {
        if (var2 == null) {
            x.d("We can do nothing with a null throwable.", new Object[0]);
            return null;
        } else {
            boolean var6;
            String var7 = (var6 = com.tencent.bugly.crashreport.crash.c.a().k()) && var3 ? " This Crash Caused By ANR , PLS To Fix ANR , This Trace May Be Not Useful![Bugly]" : "";
            if (var6 && var3) {
                x.e("This Crash Caused By ANR , PLS To Fix ANR , This Trace May Be Not Useful!", new Object[0]);
            }

            CrashDetailBean var15;
            (var15 = new CrashDetailBean()).C = com.tencent.bugly.crashreport.common.info.b.k();
            var15.D = com.tencent.bugly.crashreport.common.info.b.i();
            var15.E = com.tencent.bugly.crashreport.common.info.b.m();
            var15.F = this.d.p();
            var15.G = this.d.o();
            var15.H = this.d.q();
            var15.w = z.a(this.a, com.tencent.bugly.crashreport.crash.c.e, (String)null);
            var15.y = y.a();
            x.a("user log size:%d", new Object[]{var15.y == null ? 0 : var15.y.length});
            var15.b = var3 ? 0 : 2;
            var15.e = this.d.h();
            var15.f = this.d.k;
            var15.g = this.d.w();
            var15.m = this.d.g();
            String var8 = var2.getClass().getName();
            String var9;
            if ((var9 = b(var2, 1000)) == null) {
                var9 = "";
            }

            x.e("stack frame :%d, has cause %b", new Object[]{var2.getStackTrace().length, var2.getCause() != null});
            String var10 = "";
            if (var2.getStackTrace().length > 0) {
                var10 = var2.getStackTrace()[0].toString();
            }

            Throwable var11;
            for(var11 = var2; var11 != null && var11.getCause() != null; var11 = var11.getCause()) {
            }

            String var14;
            if (var11 != null && var11 != var2) {
                var15.n = var11.getClass().getName();
                var15.o = b(var11, 1000);
                if (var15.o == null) {
                    var15.o = "";
                }

                if (var11.getStackTrace().length > 0) {
                    var15.p = var11.getStackTrace()[0].toString();
                }

                StringBuilder var16;
                (var16 = new StringBuilder()).append(var8).append(":").append(var9).append("\n");
                var16.append(var10);
                var16.append("\n......");
                var16.append("\nCaused by:\n");
                var16.append(var15.n).append(":").append(var15.o).append("\n");
                var14 = a(var11, com.tencent.bugly.crashreport.crash.c.f);
                var16.append(var14);
                var15.q = var16.toString();
            } else {
                var15.n = var8;
                var15.o = var9 + var7;
                if (var15.o == null) {
                    var15.o = "";
                }

                var15.p = var10;
                var14 = a(var2, com.tencent.bugly.crashreport.crash.c.f);
                var15.q = var14;
            }

            var15.r = System.currentTimeMillis();
            var15.u = z.b(var15.q.getBytes());

            try {
                var15.z = z.a(com.tencent.bugly.crashreport.crash.c.f, false);
                var15.A = this.d.d;
                var15.B = var1.getName() + "(" + var1.getId() + ")";
                var15.z.put(var15.B, var14);
                var15.I = this.d.y();
                var15.h = this.d.v();
                var15.i = this.d.J();
                var15.M = this.d.a;
                var15.N = this.d.a();
                var15.Q = this.d.H();
                var15.R = this.d.I();
                var15.S = this.d.B();
                var15.T = this.d.G();
            } catch (Throwable var12) {
                x.e("handle crash error %s", new Object[]{var12.toString()});
            }

            if (var3) {
                this.b.d(var15);
            } else {
                boolean var17 = var4 != null && var4.length() > 0;
                boolean var13 = var5 != null && var5.length > 0;
                if (var17) {
                    var15.O = new HashMap(1);
                    var15.O.put("UserData", var4);
                }

                if (var13) {
                    var15.U = var5;
                }
            }

            return var15;
        }
    }

    private static boolean a(Thread var0) {
        synchronized(i) {
            if (h == null || !var0.getName().equals(h)) {
                h = var0.getName();
                return false;
            }
        }

        return true;
    }

    public final void a(Thread var1, Throwable var2, boolean var3, String var4, byte[] var5) {
        if (var3) {
            x.e("Java Crash Happen cause by %s(%d)", new Object[]{var1.getName(), var1.getId()});
            if (a(var1)) {
                x.a("this class has handled this exception", new Object[0]);
                if (this.f != null) {
                    x.a("call system handler", new Object[0]);
                    this.f.uncaughtException(var1, var2);
                } else {
                    x.e("current process die", new Object[0]);
                    Process.killProcess(Process.myPid());
                    System.exit(1);
                }
            }
        } else {
            x.e("Java Catch Happen", new Object[0]);
        }

        try {
            if (!this.g) {
                x.c("Java crash handler is disable. Just return.", new Object[0]);
                return;
            }

            if (!this.c.b()) {
                x.d("no remote but still store!", new Object[0]);
            }

            if (!this.c.c().g && this.c.b()) {
                x.e("crash report was closed by remote , will not upload to Bugly , print local for helpful!", new Object[0]);
                com.tencent.bugly.crashreport.crash.b.a(var3 ? "JAVA_CRASH" : "JAVA_CATCH", z.a(), this.d.d, var1.getName(), z.a(var2), (CrashDetailBean)null);
                return;
            }

            CrashDetailBean var11;
            if ((var11 = this.b(var1, var2, var3, var4, var5)) == null) {
                x.e("pkg crash datas fail!", new Object[0]);
                return;
            }

            com.tencent.bugly.crashreport.crash.b.a(var3 ? "JAVA_CRASH" : "JAVA_CATCH", z.a(), this.d.d, var1.getName(), z.a(var2), var11);
            if (!this.b.a(var11)) {
                this.b.a(var11, 3000L, var3);
            }

            if (var3) {
                this.b.c(var11);
            }
        } catch (Throwable var9) {
            if (!x.a(var9)) {
                var9.printStackTrace();
            }

            return;
        } finally {
            if (var3) {
                if (this.e != null && a(this.e)) {
                    x.e("sys default last handle start!", new Object[0]);
                    this.e.uncaughtException(var1, var2);
                    x.e("sys default last handle end!", new Object[0]);
                } else if (this.f != null) {
                    x.e("system handle start!", new Object[0]);
                    this.f.uncaughtException(var1, var2);
                    x.e("system handle end!", new Object[0]);
                } else {
                    x.e("crashreport last handle start!", new Object[0]);
                    x.e("current process die", new Object[0]);
                    Process.killProcess(Process.myPid());
                    System.exit(1);
                    x.e("crashreport last handle end!", new Object[0]);
                }
            }

        }

    }

    public final void uncaughtException(Thread var1, Throwable var2) {
        synchronized(i) {
            this.a(var1, var2, true, (String)null, (byte[])null);
        }
    }

    private static boolean a(UncaughtExceptionHandler var0) {
        if (var0 == null) {
            return true;
        } else {
            String var7 = var0.getClass().getName();
            String var1 = "uncaughtException";
            StackTraceElement[] var2;
            int var3 = (var2 = Thread.currentThread().getStackTrace()).length;

            for(int var4 = 0; var4 < var3; ++var4) {
                StackTraceElement var5;
                String var6 = (var5 = var2[var4]).getClassName();
                String var8 = var5.getMethodName();
                if (var7.equals(var6) && var1.equals(var8)) {
                    return false;
                }
            }

            return true;
        }
    }

    public final synchronized void a(StrategyBean var1) {
        if (var1 != null && var1.g != this.g) {
            x.a("java changed to %b", new Object[]{var1.g});
            if (var1.g) {
                this.a();
                return;
            }

            this.b();
        }

    }

    private static String a(Throwable var0, int var1) {
        if (var0 == null) {
            return null;
        } else {
            StringBuilder var2 = new StringBuilder();

            try {
                if (var0.getStackTrace() != null) {
                    StackTraceElement[] var7;
                    int var3 = (var7 = var0.getStackTrace()).length;

                    for(int var4 = 0; var4 < var3; ++var4) {
                        StackTraceElement var5 = var7[var4];
                        if (var1 > 0 && var2.length() >= var1) {
                            var2.append("\n[Stack over limit size :" + var1 + " , has been cutted !]");
                            return var2.toString();
                        }

                        var2.append(var5.toString()).append("\n");
                    }
                }
            } catch (Throwable var6) {
                x.e("gen stack error %s", new Object[]{var6.toString()});
            }

            return var2.toString();
        }
    }

    private static String b(Throwable var0, int var1) {
        if (var0.getMessage() == null) {
            return "";
        } else {
            return var0.getMessage().length() <= 1000 ? var0.getMessage() : var0.getMessage().substring(0, 1000) + "\n[Message over limit size:1000" + ", has been cutted!]";
        }
    }
}
